<%-- 
    Document   : db4
    Created on : 2014/8/19, 下午 01:26:16
    Author     : Felix
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javadb","root","root");
        Statement st = con.createStatement();//sql陳述的代表物件，使用此語法來建立
        //String sql="insert into person(username,tel,birthday) values('Tom','02-222222','2050/01/01')";
        String sql="select id, username, tel, birthday from person";//sql查詢資料庫資料語法
        
    %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%
            ResultSet rs = st.executeQuery(sql);//要把要顯示的資料庫資料語法放入此ResultSet介面
            while(rs.next())//逐一把游標往下移動每個欄位抓出資料
            {
                String id = rs.getString("id");//設定變數並把設定的每項欄位資料轉成String指定給變數
                String username = rs.getString("username");
                String tel = rs.getString("tel");
                String birthday = rs.getString("birthday");
                out.println(id+","+username+","+tel+","+birthday+"<br>");//印出資料庫欄位資料
            }
            rs.close();//使用完須關閉
            st.close();
            con.close();
            
        %>
        
    </body>
</html>
